<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Game_model extends CI_Model {

	var $title   = '';
	var $content = '';
	var $date    = '';

	function __construct()
	{ 
		parent::__construct();
	}
 
	function get_all_games($num,$offset)
	{
		$this->load->database();
		$query = $this->db->get('tbl_games',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		} 
	}
	
	function listallgames()
	{
		$this->load->database();
		$query = $this->db->get('tbl_games');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		} 
	}
	
	public function get_games_details($cateid){
		$this->load->database();
		$this->db->where('id',$cateid);
		$query = $this->db->get('tbl_games');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		} 
	}

	function total_games(){
		$this->load->database();
		return $this->db->count_all('tbl_games'); 
	}
	
	function delete_games($id)
	{
		$this->load->database();
		$this->db->delete('tbl_games', array('id' => $id));
	}

	//return 1: have banner
	//return 0: dont have banner
	function checkexit_games($game_name){
		$this->load->database();
		$this->db->where('game_name',$game_name);
		$query = $this->db->get('tbl_games');
		if ($query->num_rows() > 0)
		{
			return 1;
		} else {
			return 0;
		} 
	}
	
	// check exit banner first
	// return 1: save successfully
	// return 0: save failed
	function insert_games($game_name,$catetype,$cateroot)
	{
		$result = $this->checkexit_games($game_name);
		if($result == 0){
			$this->load->database();
			$data = array(
				'game_name' => $game_name ,                
				'game_link' =>  $game_link,
				'game_avatar' =>$game_avatar,
				'status'=>$status
			);
			$this->db->insert('tbl_games', $data);
			return 1;
		}else{
			return 0;
		}
	}
	
	//update status to active
	function update_games($id,$game_name,$game_link,$game_avatar,$status){
		  
			$this->load->database();
			$data = array(
				'game_name' => $game_name ,                
				'game_link' =>  $game_link,
				'game_avatar' =>$game_avatar,
				'status'=>$status
			);
			$this->db->where('id', $id);
			$this->db->update('tbl_games', $data);
			return 1; 
	}
	
	

	 

}